package com.cirkana.viewer.data.route.gpx;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Date;

import com.cirkana.DateUtil;
import com.cirkana.data.route.gpx.IGpxRouteWayPoint;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;
import com.cirkana.serial.bin.IBinarySerializable;

public class GpxRouteWayPoint implements IGpxRouteWayPoint, IBinarySerializable {

	private Double lat;
	private Double lon;
	private Double ele;
	private Date time;
	private String name;
	private String cmt;
	private String desc;

	// TODO: Should be protected
	public GpxRouteWayPoint() {
	}

	@Override
	public Double getLat() {
		return lat;
	}

	@Override
	public void setLat(Double lat) {
		this.lat = lat;
	}

	@Override
	public Double getLon() {
		return lon;
	}

	@Override
	public void setLon(Double lon) {
		this.lon = lon;
	}

	@Override
	public Double getEle() {
		return ele;
	}

	@Override
	public void setEle(Double ele) {
		this.ele = ele;
	}

	@Override
	public Date getTime() {
		return time;
	}

	@Override
	public void setTime(Date time) {
		this.time = time;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getCmt() {
		return cmt;
	}

	@Override
	public void setCmt(String cmt) {
		this.cmt = cmt;
	}

	@Override
	public String getDesc() {
		return desc;
	}

	@Override
	public void setDesc(String desc) {
		this.desc = desc;
	}

	@Override
	public void serialize(DataOutputStream out) throws BinarySerializationException {
		BinarySerializationUtil.serializeDouble(this.lat, out);
		BinarySerializationUtil.serializeDouble(this.lon, out);
		BinarySerializationUtil.serializeDouble(this.ele, out);
		BinarySerializationUtil.serializeDate(this.time, out);
		BinarySerializationUtil.serializeString(this.name, out);
		BinarySerializationUtil.serializeString(this.desc, out);
		BinarySerializationUtil.serializeString(this.cmt, out);
	}

	@Override
	public void deserialize(DataInputStream in) throws BinarySerializationException {
		this.lat = BinarySerializationUtil.deserializeDouble(in);
		this.lon = BinarySerializationUtil.deserializeDouble(in);
		this.ele = BinarySerializationUtil.deserializeDouble(in);
		this.time = BinarySerializationUtil.deserializeDate(in);
		this.name = BinarySerializationUtil.deserializeString(in);
		this.desc = BinarySerializationUtil.deserializeString(in);
		this.cmt = BinarySerializationUtil.deserializeString(in);
	}

	@Override
	public String toString() {
		StringBuilder s = new StringBuilder("{");
		s.append("name=");
		if (this.name == null)
			s.append("(null)");
		else
			s.append(this.name);
		s.append(",desc=");
		if (this.desc == null)
			s.append("(null)");
		else
			s.append(this.desc);
		s.append(",cmt=");
		if (this.cmt == null)
			s.append("(null)");
		else
			s.append(this.cmt);
		s.append(",lat=");
		if (this.lat == null)
			s.append("(null)");
		else
			s.append(Double.toString(this.lat));
		s.append(",lon=");
		if (this.lon == null)
			s.append("(null)");
		else
			s.append(Double.toString(this.lon));
		s.append(",ele=");
		if (this.ele == null)
			s.append("(null)");
		else
			s.append(Double.toString(this.ele));
		s.append(",time=");
		if (this.time == null)
			s.append("(null)");
		else
			s.append(DateUtil.dateFormat.format(time));
		s.append("}");

		return s.toString();
	}

}
